package in.ac.dei.edrp.cms.controller.utility;

import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class RevenueReportController extends AbstractController {
    protected ModelAndView handleRequestInternal(HttpServletRequest request,
        HttpServletResponse response) throws Exception {
        String output = ServletRequestUtils.getStringParameter(request, "output");
        StringTokenizer header = new StringTokenizer(ServletRequestUtils.getStringParameter(
                    request, "header"), "|");
        StringTokenizer data = new StringTokenizer(request.getParameter("data"),
                ";");
        String entity = ServletRequestUtils.getStringParameter(request, "entity");
        String program = ServletRequestUtils.getStringParameter(request,
                "program");
        String branch = ServletRequestUtils.getStringParameter(request, "branch");
        String specialization = ServletRequestUtils.getStringParameter(request,
                "specialization");
        String semester = ServletRequestUtils.getStringParameter(request,
                "semester");
        String courseCode = ServletRequestUtils.getStringParameter(request,
                "courseCode");
        String courseName = ServletRequestUtils.getStringParameter(request,
                "courseName");

        //		receive program-course details also
        List<String> docHeaders = new ArrayList<String>();
        docHeaders.add(entity);
        docHeaders.add(program);
        docHeaders.add(branch);
        docHeaders.add(specialization);
        docHeaders.add(semester);
        docHeaders.add(courseCode);
        docHeaders.add(courseName);

        List<String> sheetHeaders = new ArrayList<String>();

        while (header.hasMoreTokens()) {
            sheetHeaders.add(header.nextToken());
        }

        List<String> dataList = new ArrayList<String>();

        while (data.hasMoreTokens()) {
            dataList.add(data.nextToken());
        }

        List<List<String>> outputData = new ArrayList<List<String>>();
        outputData.add(docHeaders);
        outputData.add(sheetHeaders);
        outputData.add(dataList);

        if ((output == null) || "".equals(output)) {
            //return normal view
            return new ModelAndView("utility/RevenueSummary", "revenueData",
                outputData);
        } else if ("PDF".equals(output.toUpperCase())) {
            //return excel view
            return new ModelAndView("PdfRevenueSummary", "revenueData",
                outputData);
        } else {
            //return normal view
            return new ModelAndView("utility/RevenueSummary", "revenueData",
                outputData);
        }
    }
}
